package com.itboy.mall.sms.bounds.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.sms.bounds.bo.MallSpuBoundsAddBo;
import com.itboy.mall.sms.bounds.bo.MallSpuBoundsQueryBo;
import com.itboy.mall.sms.bounds.bo.MallSpuBoundsEditBo;
import com.itboy.mall.sms.bounds.domain.MallSpuBounds;
import com.itboy.mall.sms.bounds.mapper.MallSpuBoundsMapper;
import com.itboy.mall.sms.bounds.vo.MallSpuBoundsVo;
import com.itboy.mall.sms.bounds.service.IMallSpuBoundsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品spu积分设置Service业务层处理
 *
 * @author itboy
 * @date 2021-08-07
 */
@Service
public class MallSpuBoundsServiceImpl extends ServicePlusImpl<MallSpuBoundsMapper, MallSpuBounds> implements IMallSpuBoundsService {

    @Override
    public MallSpuBoundsVo queryById(Long id){
        return getVoById(id, MallSpuBoundsVo.class);
    }

    @Override
    public TableDataInfo<MallSpuBoundsVo> queryPageList(MallSpuBoundsQueryBo bo) {
        PagePlus<MallSpuBounds, MallSpuBoundsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSpuBoundsVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSpuBoundsVo> queryList(MallSpuBoundsQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSpuBoundsVo.class);
    }

    private LambdaQueryWrapper<MallSpuBounds> buildQueryWrapper(MallSpuBoundsQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSpuBounds> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSpuId() != null, MallSpuBounds::getSpuId, bo.getSpuId());
        lqw.eq(bo.getGrowBounds() != null, MallSpuBounds::getGrowBounds, bo.getGrowBounds());
        lqw.eq(bo.getBuyBounds() != null, MallSpuBounds::getBuyBounds, bo.getBuyBounds());
        lqw.eq(bo.getWork() != null, MallSpuBounds::getWork, bo.getWork());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSpuBoundsAddBo bo) {
        MallSpuBounds add = BeanUtil.toBean(bo, MallSpuBounds.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSpuBoundsEditBo bo) {
        MallSpuBounds update = BeanUtil.toBean(bo, MallSpuBounds.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSpuBounds entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
